home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / Freeware / ttengine-5.0 / Examples / DrawModes / drawmodes.c next >
Encoding:
C/C++ Source or Header  |  2002-11-05  |  10.1 KB  |  345 lines

  1. /* test ttrender */
  2.  
  3. #define __NOLIBBASE__
  4.  
  5. #include <proto/dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/intuition.h>
  8. #include <proto/graphics.h>
  9. #include <proto/layers.h>
  10. #include <proto/ttengine.h>
  11. #include <proto/asl.h>
  12.  
  13. #include <libraries/ttengine.h>
  14.  
  15.  
  16. extern struct Library *SysBase, *DOSBase;
  17. struct Library *GfxBase, *TTEngineBase, *LayersBase, *IntuitionBase, *AslBase;
  18.  
  19. ULONG BPen1, BPen2, FPen, MPen;
  20. WORD WinWidth, WinHeight;
  21.  
  22. /*----------------------------------------------------------------------------------------------------*/
  23.  
  24. static STRPTR get_font_name(struct Library *AslBase)
  25.   {
  26.     struct FileRequester *freq;
  27.     STRPTR name = NULL;
  28.  
  29.     if (freq = AllocAslRequestTags(ASL_FileRequest, TAG_END))
  30.       {
  31.         if (AslRequestTags(freq,
  32.           ASLFR_TitleText, (ULONG)"Select TrueType font",
  33.           ASLFR_InitialDrawer, (ULONG)"FONTS:",
  34.           ASLFR_DoPatterns, TRUE,
  35.           ASLFR_InitialPattern, (ULONG)"#?.ttf",
  36.           ASLFR_RejectIcons, TRUE,
  37.           TAG_END))
  38.           {
  39.             ULONG namelen = strlen(freq->fr_File) + strlen(freq->fr_Drawer) + 4;
  40.  
  41.             if (name = AllocVec(namelen + 1, MEMF_ANY | MEMF_CLEAR))
  42.               {
  43.                 strncpy(name, freq->fr_Drawer, namelen);
  44.                 AddPart(name, freq->fr_File, namelen);
  45.               }
  46.           }
  47.         FreeAslRequest(freq);
  48.       }
  49.     return name;
  50.   }
  51.  
  52. /*----------------------------------------------------------------------------------------------------*/
  53.  
  54. static VOID free_font_name(STRPTR name)
  55.   {
  56.     if (name) FreeVec(name);
  57.   }
  58.  
  59. /*----------------------------------------------------------------------------------------------------*/
  60.  
  61. VOID drawbg(struct Window *win)
  62.   {
  63.     WORD x, y;
  64.     BOOL dark;
  65.     struct RastPort *rp = win->RPort;
  66.     struct Region *rg, *org;
  67.  
  68.     if (rg = NewRegion())
  69.       {
  70.         struct Rectangle rc;
  71.  
  72.         rc.MinX = win->BorderLeft;
  73.         rc.MinY = win->BorderTop;
  74.         rc.MaxX = win->Width - win->BorderRight - 1;
  75.         rc.MaxY = win->Height - win->BorderBottom - 1;
  76.         if (OrRectRegion(rg, &rc))
  77.           {
  78.             org = InstallClipRegion(win->RPort->Layer, rg);
  79.  
  80.             for (x = 0; x < WinWidth; x += 10)
  81.               {
  82.                 dark = (x % 20) ? TRUE : FALSE;
  83.                 for (y = 0; y < WinHeight; y += 10)
  84.                   {
  85.                     if (dark) SetAPen(win->RPort, BPen1);
  86.                     else SetAPen(win->RPort, BPen2);
  87.  
  88.                     RectFill(win->RPort, x + win->BorderLeft, y + win->BorderTop,
  89.                      x + win->BorderLeft + 9, y + win->BorderTop + 9);
  90.                     dark = !dark;
  91.                   }
  92.               }
  93.           }
  94.         InstallClipRegion(win->RPort->Layer, org);
  95.         DisposeRegion(rg);
  96.       }
  97.   }
  98.  
  99. /*----------------------------------------------------------------------------------------------------*/
  100.  
  101. VOID drawtexts(struct Window *w)
  102.   {
  103.     LONG ascend, deltax, x;
  104.     struct RastPort *rp = w->RPort;
  105.  
  106.     deltax = (WinWidth - 30 >> 2) + 6;
  107.     x = w->BorderLeft + 6;
  108.  
  109.     TT_GetAttrs(rp, TT_FontAscender, (ULONG)&ascend, TAG_END);
  110. //    SetAPen(rp, FPen);
  111. //    SetBPen(rp, MPen);
  112.     TT_SetAttrs(rp, TT_Window, (ULONG)w, TAG_END);
  113.  
  114.     SetDrMd(rp, JAM1);
  115.     Move(rp, x, w->BorderTop + 6 + ascend);
  116.     TT_Text(rp, "Sample", 6);
  117.     x += deltax;
  118.  
  119.     SetDrMd(rp, JAM1 | INVERSVID);
  120.     Move(rp, x, w->BorderTop + 6 + ascend);
  121.     TT_Text(rp, "Sample", 6);
  122.     x += deltax;
  123.  
  124.     SetDrMd(rp, JAM1 | COMPLEMENT);
  125.     Move(rp, x, w->BorderTop + 6 + ascend);
  126.     TT_Text(rp, "Sample", 6);
  127.     x += deltax;
  128.  
  129.     SetDrMd(rp, JAM1 | INVERSVID | COMPLEMENT);
  130.     Move(rp, x, w->BorderTop + 6 + ascend);
  131.     TT_Text(rp, "Sample", 6);
  132.     x = w->BorderLeft + 6;
  133.  
  134.     SetDrMd(rp, JAM2);
  135.     Move(rp, x, w->BorderTop + 60 + ascend);
  136.     TT_Text(rp, "Sample", 6);
  137.     x += deltax;
  138.  
  139.     SetDrMd(rp, JAM2 | INVERSVID);
  140.     Move(rp, x, w->BorderTop + 60 + ascend);
  141.     TT_Text(rp, "Sample", 6);
  142.     x += deltax;
  143.  
  144.     SetDrMd(rp, JAM2 | COMPLEMENT);
  145.     Move(rp, x, w->BorderTop + 60 + ascend);
  146.     TT_Text(rp, "Sample", 6);
  147.     x += deltax;
  148.  
  149.     SetDrMd(rp, JAM2 | INVERSVID | COMPLEMENT);
  150.     Move(rp, x, w->BorderTop + 60 + ascend);
  151.     TT_Text(rp, "Sample", 6);
  152.     x = w->BorderLeft + 6;
  153.  
  154.     TT_SetAttrs(rp, TT_Transparency, 128, TAG_END);
  155.  
  156.     SetDrMd(rp, JAM1);
  157.     Move(rp, x, w->BorderTop + 114 + ascend);
  158.     TT_Text(rp, "Sample", 6);
  159.     x += deltax;
  160.  
  161.     SetDrMd(rp, JAM1 | INVERSVID);
  162.     Move(rp, x, w->BorderTop + 114 + ascend);
  163.     TT_Text(rp, "Sample", 6);
  164.     x += deltax;
  165.  
  166.     SetDrMd(rp, JAM1 | COMPLEMENT);
  167.     Move(rp, x, w->BorderTop + 114 + ascend);
  168.     TT_Text(rp, "Sample", 6);
  169.     x += deltax;
  170.  
  171.     SetDrMd(rp, JAM1 | INVERSVID | COMPLEMENT);
  172.     Move(rp, x, w->BorderTop + 114 + ascend);
  173.     TT_Text(rp, "Sample", 6);
  174.     x = w->BorderLeft + 6;
  175.  
  176.     SetDrMd(rp, JAM2);
  177.     Move(rp, x, w->BorderTop + 168 + ascend);
  178.     TT_Text(rp, "Sample", 6);
  179.     x += deltax;
  180.  
  181.     SetDrMd(rp, JAM2 | INVERSVID);
  182.     Move(rp, x, w->BorderTop + 168 + ascend);
  183.     TT_Text(rp, "Sample", 6);
  184.     x += deltax;
  185.  
  186.     SetDrMd(rp, JAM2 | COMPLEMENT);
  187.     Move(rp, x, w->BorderTop + 168 + ascend);
  188.     TT_Text(rp, "Sample", 6);
  189.     x += deltax;
  190.  
  191.     SetDrMd(rp, JAM2 | INVERSVID | COMPLEMENT);
  192.     Move(rp, x, w->BorderTop + 168 + ascend);
  193.     TT_Text(rp, "Sample", 6);
  194.   }
  195.  
  196. /*----------------------------------------------------------------------------------------------------*/
  197.  
  198. VOID get_window_size(struct Screen *s, APTR font)
  199.   {
  200.     LONG w;
  201.  
  202.     WinWidth = 0;
  203.     WinHeight = 222;
  204.  
  205.     if (TT_SetFont(&s->RastPort, font))
  206.       {
  207.         w = TT_TextLength(&s->RastPort, "Sample", 6);
  208.         WinWidth = (w << 2) + 30;
  209.       }
  210.     return;
  211.   }
  212.  
  213.  
  214. /*----------------------------------------------------------------------------------------------------*/
  215.  
  216. VOID loop(struct Window *w)
  217.   {
  218.     ULONG sigmask, signals;
  219.     BOOL running = TRUE;
  220.     struct IntuiMessage *imsg;
  221.  
  222.     sigmask = SIGBREAKF_CTRL_C | (1 << w->UserPort->mp_SigBit);
  223.     while (running)
  224.       {
  225.         signals = Wait(sigmask);
  226.         if (signals & SIGBREAKF_CTRL_C) running = FALSE;
  227.         if (signals & (1 << w->UserPort->mp_SigBit))
  228.           {
  229.             struct IntuiMessage *imsg;
  230.  
  231.             while (imsg = (struct IntuiMessage*)GetMsg(w->UserPort))
  232.               {
  233.                 if (imsg->Class == IDCMP_CLOSEWINDOW) running = FALSE;
  234.                 ReplyMsg((struct Message*)imsg);
  235.               }
  236.           }
  237.       }
  238.   }
  239.  
  240.  
  241. BOOL init(VOID)
  242.   {
  243.     if (!(GfxBase = OpenLibrary("graphics.library", 39))) return FALSE;
  244.     if (!(IntuitionBase = OpenLibrary("intuition.library", 39))) return FALSE;
  245.     if (!(AslBase = OpenLibrary("asl.library", 38))) return FALSE;
  246.     if (!(TTEngineBase = OpenLibrary("ttengine.library", 4))) return FALSE;
  247.     if (!(LayersBase = OpenLibrary("layers.library", 39))) return FALSE;
  248.     return TRUE;
  249.   }
  250.  
  251. VOID cleanup(VOID)
  252.   {
  253.     if (GfxBase) CloseLibrary(GfxBase);
  254.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  255.     if (AslBase) CloseLibrary(AslBase);
  256.     if (TTEngineBase) CloseLibrary(TTEngineBase);
  257.     if (LayersBase) CloseLibrary(LayersBase);
  258.   }
  259.  
  260. int Main (void)
  261.   {
  262.     struct Window *win;
  263.     STRPTR fontname;
  264.     APTR font;
  265.     struct Screen *s;
  266.  
  267.     if (init())
  268.       {
  269.         if (fontname = get_font_name(AslBase))
  270.           {
  271.             if (s = LockPubScreen(NULL))
  272.               {
  273.                 if (font = TT_OpenFont(
  274.                   TT_FontFile, (ULONG)fontname,
  275.                   TT_FontSize, 48,
  276.                 TAG_END))
  277.                   {
  278.                     free_font_name(fontname);
  279.                     get_window_size(s, font);
  280.  
  281.                     if (win = OpenWindowTags(NULL,
  282.                       WA_Top, 25,
  283.                       WA_Left, 0,
  284.                       WA_InnerWidth, WinWidth,
  285.                       WA_InnerHeight, WinHeight,
  286.                       WA_CloseGadget, TRUE,
  287.                       WA_DragBar, TRUE,
  288.                       WA_DepthGadget, TRUE,
  289.                       WA_Activate, TRUE,
  290.                       WA_IDCMP, IDCMP_CLOSEWINDOW,
  291.                       WA_Title, (ULONG)"TTEngine drawmodes",
  292.                       WA_ScreenTitle, (ULONG)"TTEngine demo",
  293.                     TAG_END))
  294.                       {
  295.                         struct RastPort *rp = win->RPort;
  296.  
  297.                         BPen1 = ObtainBestPen(win->WScreen->ViewPort.ColorMap,
  298.                          0xB5B5B5B5, 0xB5B5B5B5, 0xB5B5B5B5, TAG_END);
  299.                         drawbg(win);
  300.  
  301.                         BPen2 = ObtainBestPen(win->WScreen->ViewPort.ColorMap,
  302.                          0x61616161, 0x61616161, 0x61616161, TAG_END);
  303.                         drawbg(win);
  304.  
  305. //                        FPen = ObtainBestPen(win->WScreen->ViewPort.ColorMap,
  306. //                         0xFFFFFFFF, 0xD0D0D0D0, 0x00000000, TAG_END);
  307.  
  308. //                        MPen = ObtainBestPen(win->WScreen->ViewPort.ColorMap,
  309. //                         0x00000000, 0x4C4C4C4C, 0x80808080, TAG_END);
  310.  
  311.                         drawbg(win);
  312.  
  313.                         if (TT_SetFont(win->RPort, font))
  314.                           {
  315.                             TT_SetAttrs(rp,
  316.                               TT_Window, (ULONG)win,
  317.                               TT_Encoding, TT_Encoding_Default,
  318.                               TT_Antialias, TT_Antialias_On,
  319.                               TT_Foreground, 0x00FFD000,
  320.                               TT_Background, 0x00004C80,
  321.                             TAG_END);
  322.  
  323.                             drawtexts(win);
  324.                             loop(win);
  325.                           }
  326.                         else PutStr("TT_SetFont() failed.\n");
  327.                         ReleasePen(win->WScreen->ViewPort.ColorMap, BPen1);
  328.                         ReleasePen(win->WScreen->ViewPort.ColorMap, BPen2);
  329. //                        ReleasePen(win->WScreen->ViewPort.ColorMap, MPen);
  330. //                        ReleasePen(win->WScreen->ViewPort.ColorMap, FPen);
  331.                         TT_DoneRastPort(rp);
  332.                         CloseWindow(win);
  333.                       }
  334.                     TT_CloseFont(font);
  335.                   }
  336.                 else PutStr("Font open failed.\n");
  337.  
  338.                 UnlockPubScreen(NULL, s);
  339.               }
  340.           }
  341.       }
  342.     cleanup();
  343.     return 0;
  344.   }
  345.